#!/bin/sh 

OPSYS=`uname -s`
if [ NAME`echo $OPSYS | grep "[_-]"` != NAME ]; then
    OPSYS=`uname -o`
fi
echo "OS '$OPSYS' detected."

if [ x$OPSYS = xLinux ]; then
    TCLKIT=tclkit85-linux-x86
    TCLKIT2=tclkit85-linux-x86-2
    TCLKITSH=tclkit85-linux-x86
    OUTPUT=$1
    ZIPIT=`tar -cj $OUTPUT -f $1.tar.bz2`
elif [ x$OPSYS = "xDarwin" ]; then
    TCLKIT=tclkit85-darwin-univ
    TCLKIT2=tclkit85-darwin-univ-2
    TCLKITSH=tclkit85-darwin-univ
    OUTPUT=$1
    ZIPIT=`zip -r $1 $OUTPUT > /dev/null`
elif [ x$OPSYS = "xMsys" ]; then
    TCLKIT=tclkit-win32.exe
    TCLKIT2=tclkit-win32-2.exe
    TCLKITSH=tclkitsh-win32.exe
    OUTPUT=$1.exe
    ZIPIT=`zip -r $1 $OUTPUT > /dev/null`
fi


#
# remove old virtual file-system
#
rm -rf $1.vfs

#
# save a copy of the kit program for starting the app
#
cp $TCLKIT $TCLKIT2

#
# make a quick wrapper
#
./$TCLKITSH sdx.kit qwrap $1.tcl

#
# unwrap so we can add contents
#
./$TCLKITSH sdx.kit unwrap $1.kit

#
# add contents if any
#
if [ x$2 != x ]; then
    cp $2 $1.vfs/lib
fi
if [ x$3 != x ]; then
    cp $3 $1.vfs/lib
fi
if [ x$4 != x ]; then
    cp $4 $1.vfs/lib
fi
if [ x$5 != x ]; then
    cp $5 $1.vfs/lib
fi

#
# finish packaging
#
./$TCLKITSH sdx.kit wrap $OUTPUT -runtime $TCLKIT2

#
# now zip it
#
$ZIPIT
